package com.bestcem.xm.qdes.grpc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bestcem.xm.qdes.entity.QdesProject;
import com.bestcem.xm.qdes.grpc.v1.services.BpProjectTicketServiceGrpc;
import com.bestcem.xm.qdes.grpc.v1.services.ListProjectIdMapsRequest;
import com.bestcem.xm.qdes.grpc.v1.services.ListProjectIdMapsResponse;
import com.bestcem.xm.qdes.grpc.v1.services.ProjectIdMap;
import com.bestcem.xm.qdes.service.QdesProjectService;
import io.grpc.stub.StreamObserver;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import java.util.ArrayList;
import java.util.List;

import static com.bestcem.xm.qdes.util.QstructConstant.PROJ_EDITABLE_STATUS_LIST;

/**
 * 复制旧公司 project
 *
 * @author xiaoshu.shao <xiaoshu.shao@idiaoyan.com>
 * @version V1.0
 * @date 2022/9/21
 */
@GrpcService
@Slf4j
@RequiredArgsConstructor
public class BpProjectTicketGrpcService extends BpProjectTicketServiceGrpc.BpProjectTicketServiceImplBase {

    private @NonNull QdesProjectService projectService;

    @Override
    public void listProjectIdMaps(ListProjectIdMapsRequest request, StreamObserver<ListProjectIdMapsResponse> responseObserver) {
        String dstOrgId = request.getDstOrgId();
        String srcOrgId = request.getSrcOrgId();
        List<ProjectIdMap> idList = getProjectMap(srcOrgId, dstOrgId);

        ListProjectIdMapsResponse.Builder builder = ListProjectIdMapsResponse.newBuilder();
        builder.addAllIdMaps(idList);
        builder.setStatus(GrpcConstant.SUCCESS_STATUS);
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    private List<ProjectIdMap> getProjectMap(String srcOrgId, String dstOrgId) {
        List<ProjectIdMap> idList = new ArrayList<>();
        LambdaQueryWrapper<QdesProject> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(QdesProject::getCompanyId, srcOrgId)
            .in(QdesProject::getStatus, PROJ_EDITABLE_STATUS_LIST);
        List<QdesProject> projects = projectService.list(queryWrapper);
        if (projects == null) {
            return idList;
        }

        projects.forEach(proj -> {
            LambdaQueryWrapper<QdesProject> newWrapper = Wrappers.lambdaQuery();
            newWrapper.eq(QdesProject::getCompanyId, dstOrgId)
                .eq(QdesProject::getCode, proj.getCode())
                .eq(QdesProject::getSource, proj.getSource());
            List<QdesProject> projs = projectService.list(newWrapper);
            projs.stream().findFirst().ifPresent(newProj -> {
                ProjectIdMap.Builder proBuilder = ProjectIdMap.newBuilder();
                proBuilder.setOldId(proj.getId());
                proBuilder.setNewId(newProj.getId());
                idList.add(proBuilder.build());
            });

            /*if (CollectionUtils.isNotEmpty(projs)) {
                QdesProject newProj = projs.get(0);
                if (newProj != null) {
                    ProjectIdMap.Builder proBuilder = ProjectIdMap.newBuilder();
                    proBuilder.setOldId(proj.getId());
                    proBuilder.setNewId(newProj.getId());
                    idList.add(proBuilder.build());
                }
            }*/
        });
        return idList;
    }
}
